<?php declare(strict_types=1);

/**
 * @package SiteList (Light Portal)
 * @link https://custom.simplemachines.org/index.php?mod=4244
 * @author Bugo <bugo@dragomano.ru>
 * @copyright 2021-2025 Bugo
 * @license https://spdx.org/licenses/GPL-3.0-or-later.html GPL-3.0-or-later
 *
 * @category plugin
 * @version 26.10.25
 */

namespace LightPortal\Plugins\SiteList;

use Bugo\Compat\Config;
use Bugo\Compat\User;
use Bugo\Compat\Utils;
use LightPortal\Articles\ArticleInterface;

if (! defined('LP_NAME'))
	die('No direct access...');

class SiteArticle implements ArticleInterface
{
	private array $sites = [];

	public function init(): void
	{
		$this->sites = Utils::jsonDecode(Utils::$context['lp_site_list_plugin']['urls'] ?? '', true);
	}

	public function getSortingOptions(): array
	{
		return [];
	}

	public function getData(int $start, int $limit, ?string $sortType): iterable
	{
		foreach ($this->sites as $url => $data) {
			$item = [
				'title'     => $data[1] ?: $url,
				'is_new'    => false,
				'edit_link' => Config::$scripturl . '?action=admin;area=lp_plugins',
				'can_edit'  => User::$me->is_admin,
				'link'      => $url,
				'teaser'    => $data[2] ?? '',
				'image'     => $data[0] ?: ('https://image.thum.io/get/' . $url)
			];

			yield $url => $item;
		}
	}

	public function getTotalCount(): int
	{
		if (empty($this->sites))
			return 0;

		return count($this->sites);
	}
}
